package chapter13.eg1.listener;

import chapter13.eg1.broadcaster.LogEvent;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.util.CharsetUtil;
import java.util.List;

public class LogEventDecoder extends MessageToMessageDecoder<DatagramPacket> {


  @Override
  protected void decode(ChannelHandlerContext ctx, DatagramPacket datagramPacket, List<Object> out)
      throws Exception {
    ByteBuf data = datagramPacket.content();
    int index = data.indexOf(0, data.readableBytes(), LogEvent.SEPARATOR);
    String filename = data.slice(0, index).toString(CharsetUtil.UTF_8);
    String logMsg = data.slice(index + 1, data.readableBytes()).toString(CharsetUtil.UTF_8);
    LogEvent event = new LogEvent(datagramPacket.sender(), filename, logMsg, System.currentTimeMillis());
    out.add(event);
  }
}
